﻿namespace DimensionsHelper.Client.WinUi.Views;

public sealed partial class QueryDataPage : Page, INavigationAware
{
    public QueryDataPage()
    {
        ViewModel = App.GetService<QueryDataViewModel>();
        InitializeComponent();
    }

    public QueryDataViewModel ViewModel
    {
        get;
    }

    public void OnNavigatedTo(object parameter)
    {
        string? tabName = parameter.ToString();

        if (string.IsNullOrEmpty(tabName))
        {
            return;
        }

        TabViewItem? item = DataTabView.TabItems.OfType<TabViewItem>().FirstOrDefault(i => i.Header.Equals(tabName));

        if (item != null)
        {
            DataTabView.SelectedItem = item;
        }
    }

    public void OnNavigatedFrom()
    {
    }

    private void TabViewItem_CloseRequested(TabViewItem sender, TabViewTabCloseRequestedEventArgs args) =>
        ViewModel.RemoveDataTable(sender.Header as string);
}